package com.kexio.user.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.kexio.user.entity.RoleMenu;

/**
 * 角色菜单关联Mapper接口
 * 
 * @author kexio
 * @since 1.0.0
 */
@Mapper
public interface RoleMenuMapper extends BaseMapper<RoleMenu> {
    
    /**
     * 根据角色编码查询菜单ID列表
     */
    List<String> selectMenuIdsByRoleCodeAndTenant(@Param("roleCode") String roleCode);
    
    /**
     * ✅ 批量查询多个角色的菜单ID列表（避免N+1查询）
     */
    @org.apache.ibatis.annotations.Select("<script>" +
            "SELECT DISTINCT rm.menu_id " +
            "FROM sys_role_menu rm " +
            "INNER JOIN sys_role r ON rm.role_id = r.id " +
            "WHERE r.code IN " +
            "<foreach collection='roleCodes' item='roleCode' open='(' separator=',' close=')'>" +
            "#{roleCode}" +
            "</foreach>" +
            " AND r.status = 1" +
            "</script>")
    List<String> selectMenuIdsByRoleCodesAndTenant(@Param("roleCodes") List<String> roleCodes);
    
    /**
     * 根据菜单ID查询角色ID列表
     */
    List<String> selectRoleIdsByMenuIdAndTenant(@Param("menuId") String menuId);
    
    /**
     * 批量插入角色菜单关联
     */
    int batchInsert(@Param("roleMenus") List<RoleMenu> roleMenus);
    
    /**
     * 根据角色ID删除菜单关联
     */
    int deleteByRoleIdAndTenant(@Param("roleId") String roleId, 
                                @Param("tenantId") String tenantId);
    
    /**
     * 根据菜单ID删除角色关联
     */
    int deleteByMenuIdAndTenant(@Param("menuId") String menuId, 
                                @Param("tenantId") String tenantId);
}
